package com.ereport.controller.expensesController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.OsExpensesDAO;
import com.ereport.entity.Executives;
import com.ereport.entity.Expenses;
import com.ereport.entity.LocalExpenses;
import com.ereport.entity.MblExpenses;
import com.ereport.entity.OutStationExpenses;

@Controller
@RequestMapping("/expenses")
public class OSExpensesController {

	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		model.setViewName("/jsp/expenses.jsp");
		return model;
	}
	
	@RequestMapping(value="/mblsave.do", method=RequestMethod.POST)
	public ModelAndView mblinsert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String mblMnth=req.getParameter("mbMonth");
		String mblYr=req.getParameter("mbYear");
		String spend=req.getParameter("spend");
		String remarks=req.getParameter("remarks");
		Date cdate=new Date();
		cdate=new java.sql.Date(cdate.getTime());
		OsExpensesDAO osdao=new OsExpensesDAO();
		try {
			osdao.mblsave(new MblExpenses(ex,mblMnth,mblYr,spend,remarks,cdate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/outsave.do", method=RequestMethod.POST)
	public ModelAndView outinsert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String frmDate=req.getParameter("otexpnsesDate1");
		DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
		Date fmDate=null;
		try {
			fmDate = df.parse(frmDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String toDate=req.getParameter("otexpnsesDate2");
		Date tdate=null;
		try {
			tdate = df.parse(toDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String particulars=req.getParameter("particulars");
		String entertainment=req.getParameter("entertainment");
		String travel=req.getParameter("travel");
		String hotel=req.getParameter("hotel");
		String others=req.getParameter("others");
		Date cdate=new Date();
		cdate=new java.sql.Date(cdate.getTime());
		OsExpensesDAO osdao=new OsExpensesDAO();
		try {
			osdao.outsave(new OutStationExpenses(ex,new java.sql.Date(fmDate.getTime()),new java.sql.Date(tdate.getTime()),particulars, entertainment,travel,hotel,others,cdate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/localsave.do", method=RequestMethod.POST)
	public ModelAndView localinsert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String lDate=req.getParameter("expnsesDate");
		String kms=req.getParameter("trLtr");
		String fromloc=req.getParameter("trFrom");
		String perkms=req.getParameter("trPerLtr");
		String toloc=req.getParameter("trTo");
		String trTotal=req.getParameter("trTotal");
		float km=0;
		if(kms.length()!=0){
			km= Float.parseFloat(kms);
		}
		
		float perkm=0;
		if(perkms.length()!=0){
			perkm=Float.parseFloat(perkms);
		}
		float totalExp=0;
		if(trTotal.length()!=0){
			totalExp=Float.parseFloat(trTotal);
		}
		Date cdate=null;
		DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
		try {
			cdate=df.parse(lDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		cdate=new java.sql.Date(cdate.getTime());
		OsExpensesDAO osdao=new OsExpensesDAO();
		try {
			osdao.localsave(new LocalExpenses(ex,km,perkm,fromloc,toloc,totalExp,cdate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/getDetails.do", method=RequestMethod.GET)
	public @ResponseBody
	String getDetails(HttpServletRequest req,HttpSession session){
		Executives ex=(Executives)session.getAttribute("loginUser");
		int exeId=ex.getId();
		String osdate=req.getParameter("expdate");
		System.out.println("date is "+osdate);
		DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
		Date d=null;
		try {
			d = df.parse(osdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date dt=new java.sql.Date(d.getTime());
		System.out.println(dt);
		OsExpensesDAO os=new OsExpensesDAO();
		Expenses ose=null;
		try {
			ose=os.getDetails(exeId,dt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(ose!=null){
		StringBuilder sb=new StringBuilder();
		sb.append(ose.getFromloc()+"/");
		sb.append(ose.getToloc()+"/");
		sb.append(ose.getLiters()+"/");
		sb.append(ose.getPerliter()+"/");
		sb.append(ose.getParticulars()+"/");
		sb.append(ose.getTravel()+"/");
		sb.append(ose.getHotel()+"/");
		sb.append(ose.getEntertainment()+"/");
		sb.append(ose.getOthers()+"/");
		sb.append(ose.getMblimit()+"/");
		sb.append(ose.getSpend()+"/");
		sb.append(ose.getRemarks());
		
		return sb.toString();
		}
		return "";
	}
	@RequestMapping(value="/getLocalConvyList.do")
	public ModelAndView getLocalConvyList(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		int eid=ex.getId();
		if(ex.isIs_admin()==true){
			eid=Integer.parseInt(req.getParameter("exeName"));
		}
		OsExpensesDAO os=new OsExpensesDAO();
		List<LocalExpenses> locallist=new ArrayList<LocalExpenses>();
		try {
			 locallist=os.getLocalList(eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/localConvyList.jsp");
		model.addObject("explist", locallist);
		return model;
	}
	@RequestMapping(value="/getOutStationList.do")
	public ModelAndView getOutStationList(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		int eid=ex.getId();
		if(ex.isIs_admin()==true){
			eid=Integer.parseInt(req.getParameter("exeName"));
		}
		OsExpensesDAO os=new OsExpensesDAO();
		List<OutStationExpenses> locallist=new ArrayList<OutStationExpenses>();
		try {
			locallist=os.getOutStationList(eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/outStationList.jsp");
		model.addObject("explist", locallist);
		return model;
	}
	@RequestMapping(value="/getMblList.do")
	public ModelAndView getMblList(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		int eid=ex.getId();
		if(ex.isIs_admin()==true){
			eid=Integer.parseInt(req.getParameter("exeName"));
		}
		OsExpensesDAO os=new OsExpensesDAO();
		List<MblExpenses> locallist=new ArrayList<MblExpenses>();
		try {
			locallist=os.getMblList(eid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/mblExpnsList.jsp");
		model.addObject("explist", locallist);
		return model;
	}
}
